package lumis.doui.control.principalpicker;

import lumis.doui.control.DataBoundControl;
import lumis.doui.control.IClientSideReadableControl;
import lumis.doui.source.field.type.PrincipalDataType;
import lumis.portal.PortalException;
import lumis.portal.principal.PrincipalConfig;
import lumis.portal.servicecontainer.ServiceContainerUrl;
import lumis.portal.serviceinterface.IServiceInterfaceUrl;
import lumis.util.IResource;
import lumis.util.XmlUtil;

import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.Node;

/**
 * Renders a control that allows the end user to select a principal. 
 * The list of principals may be limited to users or groups.
 * <br/>
 * <br/> 
 * Example use:
 * <xmp>
 * <control type="lum_principalPicker" id="principalId" showType="users" />
 * </xmp>
 * 
 * xml data available for xsl rendering:
 * <xmp>
 * <control id="principalId" showType="users" type="lum_principalPicker">
 * 	<strings>
 * 		<string id="STR_SELECT">Select</string>
 * 		<string id="STR_CLEAR">Clear</string>
 * 	</strings>
 * 	<data>
 * 		<principal>
 * 			<principalId>00000000D00000000000000000000001</principalId>
 * 			<shortId>admin</shortId>
 * 			<name>Administrator</name>
 * 			<type>1</type>
 * 			<subtype>0</subtype>
 * 		</principal>
 * 		<openPopup><!--javascript--></openPopup>
 * 		<updateScript><!--javascript--></updateScript>
 * 	</data>
 * </control>
 * </xmp>
 * @version $Revision: 11835 $ $Date: 2010-08-05 16:29:01 -0300 (Thu, 05 Aug 2010) $
 * @since 4.1.1
 */
public class PrincipalPickerControl extends DataBoundControl implements IClientSideReadableControl
{
	protected static final String SHOW_USERS = "users";
	protected static final String SHOW_GROUPS= "groups";
	protected static final String SHOW_ALL= "all";
	
	@Override
	public void buildSubControls() throws PortalException
	{
		super.buildSubControls();
		
		autoAddValidators();
	}
	
	@Override
	protected Class<? extends PrincipalDataType.Data> getValueClass() throws PortalException
	{
		return PrincipalDataType.Data.class;
	}

	@Override
	public void setRenderData() throws PortalException
	{
		super.setRenderData();

		Node dataNode = XmlUtil.addNode(controlDefinitionNode, "data");
		
		PrincipalDataType.Data principalData = (PrincipalDataType.Data)getValue();
		if(principalData!=null)
		{
			String principalXml = "<principal>" + principalData.getRenderData(this) + "</principal>";
			XmlUtil.addNodeFromXmlString(dataNode, principalXml);
		}
		
		IServiceInterfaceUrl popupUrl = getPopupUrl();
		XmlUtil.addTextNode(dataNode, "openPopup", ((ServiceContainerUrl)popupUrl).getWindowOpen(douiContext.getTransaction()) + ";");
		XmlUtil.addTextNode(dataNode, "updateScript", ";");
	}
	
	protected IServiceInterfaceUrl getPopupUrl() throws PortalException
	{
		IServiceInterfaceUrl popupUrl = douiContext.getResponse().createRenderURL();
		// since this interface doesn't need a service instance, we pass it null
		popupUrl.setRunTimeServiceInstanceId(null);
		popupUrl.setRunTimeServiceInterfaceId("lumis.service.doui.principalpicker.principalPicker");
		popupUrl.setParameter("callbackFunction", "select_" + douiContext.getRequest().getServiceInterfaceInstanceId() + 
				"_" + getId().replace('.', '_'));
		
		String show = XmlUtil.readAttributeString("showType", controlDefinitionNode, "all");
		if (SHOW_USERS.equals(show))
		{
			popupUrl.setParameter("showType", Integer.toString(PrincipalConfig.TYPE_USER));
		}
		else if (SHOW_GROUPS.equals(show))
		{
			popupUrl.setParameter("showType", Integer.toString(PrincipalConfig.TYPE_GROUP));
		}

		return popupUrl;
	}

	@Override
	protected Map<String, String> getStringsToLocalize()
	{
		HashMap<String, String> strings = new HashMap<String, String>();
		strings.put("STR_SELECT", "STR_SELECT");
		strings.put("STR_CLEAR", "STR_CLEAR");
		return strings;
	}
	
	@Override
	public IResource getControlResource() throws PortalException
	{
		if (controlResource == null)
		{
			controlResource = super.getControlResource();
			controlResource.addResourcePath("lumis/content/control/document/strings/strings");
			controlResource.addResourcePath("lumis/content/strings/strings");
		}

		return controlResource;
	}

	@Override
	protected boolean fixedIdentifierRequired()
	{
		return true;
	}
}
